<template>
  <div class="comment-list-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="内容标题">
          <el-input
            v-model="searchForm.contentTitle"
            placeholder="请输入内容标题"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="评论用户">
          <el-input
            v-model="searchForm.username"
            placeholder="请输入用户名"
            clearable
            style="width: 150px"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 120px">
            <el-option label="正常" value="1" />
            <el-option label="已删除" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作区域 -->
    <el-card class="operation-card">
      <el-button type="danger" :disabled="!multipleSelection.length" @click="handleBatchDelete">
        <el-icon><Delete /></el-icon>
        批量删除
      </el-button>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="contentTitle" label="内容标题" min-width="200" show-overflow-tooltip />
        <el-table-column prop="username" label="评论用户" width="120" />
        <el-table-column prop="content" label="评论内容" min-width="250" show-overflow-tooltip />
        <el-table-column prop="likeCount" label="点赞数" width="100" />
        <el-table-column label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '正常' : '已删除' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="评论时间" width="180" />
        <el-table-column label="操作" fixed="right" width="150">
          <template #default="scope">
            <el-button
              v-if="scope.row.status === 1"
              type="warning"
              size="small"
              @click="handleToggleStatus(scope.row)"
            >
              删除
            </el-button>
            <el-button
              v-else
              type="success"
              size="small"
              @click="handleToggleStatus(scope.row)"
            >
              恢复
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              永久删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 评论详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="评论详情"
      width="600px"
    >
      <div v-if="currentComment" class="comment-detail">
        <div class="detail-item">
          <label>内容标题：</label>
          <span>{{ currentComment.contentTitle }}</span>
        </div>
        <div class="detail-item">
          <label>评论用户：</label>
          <span>{{ currentComment.username }}</span>
        </div>
        <div class="detail-item">
          <label>评论内容：</label>
          <div class="comment-content">{{ currentComment.content }}</div>
        </div>
        <div class="detail-item">
          <label>点赞数：</label>
          <span>{{ currentComment.likeCount }}</span>
        </div>
        <div class="detail-item">
          <label>评论时间：</label>
          <span>{{ currentComment.createTime }}</span>
        </div>
        <div class="detail-item">
          <label>状态：</label>
          <el-tag :type="currentComment.status === 1 ? 'success' : 'danger'">
            {{ currentComment.status === 1 ? '正常' : '已删除' }}
          </el-tag>
        </div>
      </div>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 响应式数据
const loading = ref(false)
const detailDialogVisible = ref(false)
const tableData = ref([])
const multipleSelection = ref([])
const currentComment = ref(null)

// 搜索表单
const searchForm = reactive({
  contentTitle: '',
  username: '',
  status: ''
})

// 分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 生命周期
onMounted(() => {
  fetchCommentList()
})

// 获取评论列表
const fetchCommentList = async () => {
  loading.value = true
  try {
    // 模拟API调用
    setTimeout(() => {
      const mockData = [
        {
          id: 1,
          contentTitle: '如何提高认知能力：从思维模式开始',
          username: 'user001',
          content: '这篇文章写得很好，对我很有启发。特别是关于思维模式的部分，让我重新思考了自己的认知方式。',
          likeCount: 15,
          status: 1,
          createTime: '2023-01-01 10:30:00'
        },
        {
          id: 2,
          contentTitle: '健身入门指南：从零开始的运动计划',
          username: 'user002',
          content: '作为一个健身新手，这个指南对我来说太有用了！按照这个计划执行了一个月，效果很明显。',
          likeCount: 23,
          status: 1,
          createTime: '2023-01-02 14:20:00'
        },
        {
          id: 3,
          contentTitle: '高效学习方法论：如何快速掌握新技能',
          username: 'user003',
          content: '垃圾文章，浪费时间！',
          likeCount: 0,
          status: 0,
          createTime: '2023-01-03 16:45:00'
        }
      ]
      tableData.value = mockData
      pagination.total = mockData.length
      loading.value = false
    }, 500)
  } catch (error) {
    console.error('获取评论列表失败:', error)
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.currentPage = 1
  fetchCommentList()
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 查看评论详情
const handleViewDetail = (row) => {
  currentComment.value = row
  detailDialogVisible.value = true
}

// 删除评论
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要永久删除这条评论吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟删除操作
    ElMessage.success('删除成功')
    fetchCommentList()
  }).catch(() => {
    // 取消删除
  })
}

// 批量删除
const handleBatchDelete = () => {
  ElMessageBox.confirm(`确定要删除选中的 ${multipleSelection.value.length} 条评论吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟批量删除操作
    ElMessage.success('批量删除成功')
    fetchCommentList()
  }).catch(() => {
    // 取消删除
  })
}

// 切换评论状态
const handleToggleStatus = (row) => {
  const action = row.status === 1 ? '删除' : '恢复'
  ElMessageBox.confirm(`确定要${action}这条评论吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 模拟状态切换操作
    row.status = row.status === 1 ? 0 : 1
    ElMessage.success(`${action}成功`)
  }).catch(() => {
    // 取消操作
  })
}

// 表格选择变化
const handleSelectionChange = (selection) => {
  multipleSelection.value = selection
}

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.pageSize = size
  fetchCommentList()
}

// 当前页变化
const handleCurrentChange = (page) => {
  pagination.currentPage = page
  fetchCommentList()
}
</script>

<style scoped>
.comment-list-container {
  padding: 20px;
}

.search-card,
.operation-card,
.table-card {
  margin-bottom: 20px;
}

.search-form {
  margin-bottom: 0;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}

.comment-detail {
  padding: 10px 0;
}

.detail-item {
  margin-bottom: 15px;
  display: flex;
  align-items: flex-start;
}

.detail-item label {
  width: 80px;
  font-weight: bold;
  color: #606266;
  flex-shrink: 0;
}

.comment-content {
  background: #f5f7fa;
  padding: 10px;
  border-radius: 4px;
  line-height: 1.6;
  flex: 1;
}
</style>